<?php
/**
 * 调用此框架前需要先定义常量: 项目根目录的绝对路径.
 */
defined('ROOT_DIR') || exit('Request Error, No defined ROOT_DIR');
/**
 * 调用此框架前需要先定义常量: 项目名称.
 */
defined('APP_NAME') || exit('Request Error, No defined APP_NAME');

defined('SYS_BEGIN_TIME') || define('SYS_BEGIN_TIME', microtime(true));
defined('SYS_DEBUG') || define('SYS_DEBUG', false);
defined('DS') || define('DS', DIRECTORY_SEPARATOR);

/**
 * 常量: 框架所在目录的绝对路径.
 */
defined('FRAMEWORK_DIR') || define('FRAMEWORK_DIR', dirname(__FILE__));
/**
 * 常量: 项目所在目录的绝对路径.
 */
defined('APP_DIR') || define('APP_DIR', ROOT_DIR . DS . APP_NAME);

define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
error_reporting(SYS_DEBUG ? E_ALL : 0);
date_default_timezone_set('Asia/Shanghai');
set_magic_quotes_runtime(0);

/**
 * CLoader class file.
 * 常用方法. 自动加载常用的类.
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CLoader.php 1 2011-01-06 20:33:26Z huan.song $
 * @since 2.0
 */
class CLoader
{
	private static $_includePaths;
	
	/**
	 * @return string 框架的版本号.
	 */
	public static function getVersion()
	{
		return '2.0.0';
	}
	
	/**
	 * 自动加载类文件.
	 * @param string $class 类名.
	 * @return void.
	 * @throws CException. 如果无法设置php.ini的include_path, 抛出异常.
	 */
	public static function autoload($class)
	{
		if (isset(self::$_coreClasses[$class])) 
			return require FRAMEWORK_DIR . self::$_coreClasses[$class];
		
		if (null === self::$_includePaths)
		{
			self::$_includePaths = array_unique(explode(PATH_SEPARATOR, trim(get_include_path(), PATH_SEPARATOR)));
			if (is_array(CConfig::get('import')))
				self::$_includePaths = array_merge(self::$_includePaths, CConfig::get('import'));
				
			if (false === set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)))
				throw new CException('CLoader:autoload your server configuration not allowed to change PHP include_path', array('{include_path}', '.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)));
		}
		
		return require $class . '.php';
	}
	
	/**
	 * @var array 框架中所有核心类.
	 */
	private static $_coreClasses=array(
		'CConfig' => '/base/CConfig.php',
		'CDbConfig' => '/db/CDbConfig.php',
		'CControllerConfig' => '/controller/CControllerConfig.php',
		'CModelConfig' => '/model/CModelConfig.php',
		'CViewConfig' => '/views/CViewConfig.php',
		
		'CApplication' => '/base/CApplication.php',
		'CRequest' => '/controller/CRequest.php',
		'CDispatcher' => '/controller/CDispatcher.php',
		'CController' => '/controller/CController.php',
		'CDbDriver' => '/db/CDbDriver.php',
		'CDbStatement' => '/db/CDbStatement.php',
		'CDbTable' => '/db/CDbTable.php',
	
		'CMysqlTable' => '/db/mysql/CMysqlTable.php',
		'CMysqlSchema' => '/db/mysql/CMysqlSchema.php',
		'CMysqlTableSchema' => '/db/mysql/CMysqlTableSchema.php',
		'CMysqlColumnSchema' => '/db/mysql/CMysqlColumnSchema.php',
		
		'CModel' => '/model/CModel.php',
		
		'CLog' => '/log/CLog.php',
		'CLogDb' => '/log/CLogDb.php',
		'CLogStream' => '/log/CLogStream.php',
		'CLogException' => '/log/CLogException.php',
	
		'CRegistry' => '/helpers/CRegistry.php',
		'CString' => '/helpers/CString.php',
		'CPagelist' => '/helpers/CPagelist.php',
		'CImage' => '/helpers/CImage.php',
	
		'CException' => '/base/CException.php',
		'CConfigException' => '/base/CConfigException.php',
		'CDbException' => '/db/CDbException.php',
		'CControllerException' => '/controller/CControllerException.php',
		'CModelException' => '/model/CModelException.php',
		'CViewException' => '/views/CViewException.php',
		'CHttpException' => '/base/CHttpException.php',
	);
}

require FRAMEWORK_DIR . '/base/interfaces.php';
spl_autoload_register(array('CLoader', 'autoload'));
